<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" >
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <span>状态</span>
          <el-select v-model="value" clearable placeholder="请选择">
            <el-option
                v-for="item in options"
                :label="item.state"
                :value="item.value">
            </el-option>
          </el-select>
          <el-button type="primary" v-on:click="getKeyword">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--
      :data="menu"
      highlight-current-row
      v-loading="listLoading"
       @selection-change="selsChange"
       style="width: 100%
    -->
    <!--列表-->
    <el-table :data="menu" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="150" >
      </el-table-column>
      <el-table-column prop="component" label="组件" width="150" >
      </el-table-column>
      <el-table-column prop="url" label="路径" width="150" >
      </el-table-column>
      <el-table-column   prop="parentName" label="上级菜单" width="100" >
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80" >
        <template slot-scope="scope">
          <span v-if="scope.row.state" style="color: green">启用</span>
          <span v-else style="color: red">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标" min-width="100" >
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--添加和修改模态框-->
    <!--
      :title="title"  - 标题
      :visible.sync="saveFormVisible" - 是否显示对画框 - 默认不显示
      :close-on-click-modal="false" - 在对话框外面点击不关闭

      :model="saveForm"  -  绑定表单
      label-width="80px" -
      :rules="saveFormRules" - 表单的验证规则
      ref="saveForm" - 为当前对话框取名 - 方便验证表单
    -->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="100px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="菜单" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件" prop="component">
          <el-input v-model="saveForm.component" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="主路径" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="index">
          <el-input-number v-model="saveForm.index" auto-complete="off"></el-input-number>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-cascader clearable v-model="saveForm.parentId"
                       :options="menuTree"
                       :props="{
            	checkStrictly: true,
                label: 'name', // 级联框显示那个属性的值
                value: 'id'    // 回显时需要,回传值的时候也需要
            }" clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="true">启用</el-radio>
            <el-radio class="radio" :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>

export default {
  data() {
    return {

      //店铺列表
      menu: [],
      //忙等框
      listLoading: false,
      //分页参数
      currentPage:1,
      pageSize:5,
      totals: 0,

      //高级查询
      //关键字
      keyword:null,
      value:null,

      options: [{
        value: '1',
        state: '启用'
      }, {
        value: '0',
        state: '禁用'
      }],
      sels: [],//列表选中列
      //新增或修改
      "title":'',
      saveFormVisible: false,//添加和修改对话框是否显示
      saveLoading: false,  //保存的忙等框 - 在提交按钮上
      saveFormRules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ],
        component:[
          { required: true, message: '请输入组件名称', trigger: 'blur' }
        ],
        url:[
          { required: true, message: '请输入主路径', trigger: 'blur' }
        ],
      },
      //编辑界面数据
      saveForm: {
        id: null,
        name: '',
        component: '',
        url: '',
        index: 1,
        parentId:null,
        state: true,
      },
      //查询菜单树
      menuTree:[],
    }
  },
  methods: {
    //获取部门列表
    getMenu() {
      let para = {
        //传入当前页数，当前页条数，高级查询参数
        "currentPage":this.currentPage,
        "pageSize":this.pageSize,
        "keyword":this.keyword,
        "state":this.value,
      };
      //开启盲等框
      this.listLoading = true;
      //发送post请求
      this.$http.post("/menu",para).then(res=>{
        //获取后端返回的数据
        this.menu=res.data.data;
        this.totals=res.data.totals;
        this.listLoading=false;
      }).catch(res=>{
        //弹框提示
        this.$message.error("网络繁忙，请稍后再试，【查询列表】");
      })
    },
    //2.分页
    handleCurrentChange(val) {
      //获取当前页码
      this.currentPage = val;
      //调用查询方法
      this.getMenu();
    },

    //3.高级查询
    getKeyword(){
      //返回到第一页
      this.currentPage=1;
      //调用查询
      this.getMenu();
    },

    //4.删除
    handleDel: function (index, row) {
      //提示删除弹框
      this.$confirm('确认删除该记录吗?', '危险提示', {
        type: 'warning'
      }).then(() => {
        //开启盲等框
        this.listLoading = true;
        //delete请求
        this.$http.delete("/menu/"+row.id).then(res=>{
          //关闭盲等框
          this.listLoading = false;
          //判断是否删除成功
          if(res.data.success){
            //成功提示
            this.$message.success("删除成功");
            //删除之前的总页数 也就是最后一页
            let totalPage = Math.ceil(this.totals/this.pageSize);
            //如果当前页是最后一页 并且 总条数减一膜上每页显示的条数等于0 也就是说删除的条数是当前页的最后一条  并且 当前页不是第一页
            if(this.currentPage==totalPage && (this.totals-1)%this.pageSize==0 && this.currentPage!=1){
              //就把当前页的页码减一
              this.currentPage = this.currentPage-1;
            }
            //对当前页处理
            this.getMenu();
          }else {
            //后端报错500
            this.$message.error("网络繁忙 【500】")
          }
        }).catch(res=>{
          //400 403 等一些错误
          this.$message.error("网络繁忙【400】")
        })
      }).catch(() => {//点击取消

      });
    },
    //5.批量删除
    selsChange: function (sels) {
      //选中就会添加到这个对象的内容数组里面
      this.sels = sels;
    },
    batchRemove: function () {
      //把获取到的数组对象装换成只有id的数组
      var ids = this.sels.map(item => item.id);
      //提示删除弹框
      this.$confirm('确认删除选中记录吗？', '危险提示', {
        type: 'warning'
      }).then(() => {
        //开启盲等框
        this.listLoading = true;
        //发送patch请求  批量操作
        this.$http.patch("/menu",ids).then(res=>{
          //关闭忙等框
          this.listLoading=false;
          if(res.data.success){
            this.$message.success("删除成功");
            //跳转到第一页
            this.currentPage=1;
            this.getMenu();
          }else {
            this.$message.error("网络繁忙，请稍后再试【500】");
          }
        }).catch(res=>{
          this.$message.error("网络繁忙，请稍后再试 【400】");
        })
      }).catch(() => {

      });
    },
    //显示新增界面
    handleAdd: function () {
      //重置表单
      this.saveForm={
        id: null,
        name: '',
        component: '',
        url: '',
        index: 1,
        parentId:null,
        state: true,
        icon:''
      };
      //修改标题为添加
      this.title="添加";
      this.getMenuTree();
      //打开添加框
      this.saveFormVisible = true;
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      //回显数据
      this.saveForm = Object.assign({}, row);
      //标题改为修改
      this.title="修改";
      this.getMenuTree();
      //打开修改框
      this.saveFormVisible = true;
    },

    //添加或修改
    saveSubmit: function () {
      //验证
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //打开确认提交框
            this.saveLoading = true;
            //克隆输入的值
            let para = Object.assign({}, this.saveForm);
            let arr = para.parentId;
            //如果id长度为0，或者为空，就赋值null
            if(arr && arr.length==0){
              para.parentId = null;
            }
            //如果大于0，就拿到最后一个索引的值
            if(arr && arr.length>0){ //[1,13]
              para.parentId = arr[arr.length-1];
            }

            this.$http.put("/menu",para).then(res=>{
              //关闭忙等框
              this.saveLoading=false;
              //关闭添加修改框
              this.saveFormVisible=false;
              if(res.data.success){
                this.$message.success("操作成功");
                this.getMenu();
              }else {
                this.$message.error("网络繁忙，请稍后再试【500】");
              }
            }).catch(res=>{
              this.$message.error("网络繁忙，请稍后再试 【400】");
            })
          });
        }
      });
    },
    getMenuTree(){
      this.$http.get("/menu/menuTree").then(res=>{
        this.menuTree = res.data;
        console.log(res.data);
      }).catch(res=>{

      })
    }

  },
  //页面渲染完成后调用此方法
  mounted() {
    this.getMenu();
  }
}

</script>

<style scoped>

</style>
